/*
 * tim.c
 *
 *  Created on: May 8, 2022
 *      Author: Abyss
 */


#include "config.h"


//定时器回调
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  UNUSED(htim);
  //串口空闲接收
  if(htim == &htim17)
  {

	  HAL_TIM_Base_Stop(&htim17);
	  buffer_flag = FLAG_TRUE;

	  HAL_UART_Transmit(&huart1, buffers, sizeof(buffers), 1000);

	  //判断是否返回TCMQTTRCVPUB数据
	  if(strstr(buffers,"+TCMQTTRCVPUB")!=0)
	  {
		  TCMQTTRCVPUB = FLAG_TRUE;
			//处理接收数据
				char power_switch[16];

				char *str = strstr(buffers,"\"power_switch");

				for(int x=0;x<16;x++)
				{
					power_switch[x] = *(str++);
				}

				if(power_switch[15]=='0')
				{
					HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, RESET);
					all_data.power_switch = RESET;
					WS281x_SetRGB(GREEN);
				}
				else if(power_switch[15]=='1')
				{
					HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, SET);
					all_data.power_switch = SET;
					WS281x_SetRGB(RED);
				}
	  }


	  //如果已连接腾讯云 拦截数据
	  if(_TC == TC_CONNECT)
	  {

		  buffer_flag = FLAG_FLASE;
		  memset(buffers,0,sizeof(buffers));
		  buffer_cnt = 0;
	  }
  }
  //提供软件计时  用于超时程序
  if(htim == &htim16)
  {
	  sys_time++;
  }

  if(htim == &htim15)
  {
	  //状态寄存器  检测寄存器   数据更新寄存器
	  uint8_t State_REG,Check_REG,Data_Updata_REG,CheckSum_REG;
	  //PF 寄存器
	  uint16_t PF_REG;
	  //电压参数寄存器  电压寄存器    电流参数寄存器   电流寄存器   功率参数寄存器   功率寄存器
	  uint32_t Voltage_parameter_REG,Voltage_REG,Current_PParameter_REG,Current_REG,Power_parameter_REG,Power_REG;
	  HAL_TIM_Base_Stop(&htim15);

	 // HAL_UART_Transmit(&huart1, electric, 24, 1000);
//      if(all_data.power_switch == SET)
//      {
    	  //接收数据解析
    	  if(electric[1]==0x5a)
    	  {
    		  State_REG = electric[0];
    		  Check_REG = electric[1];

    		  Voltage_parameter_REG =  electric[2];
    		  Voltage_parameter_REG = (Voltage_parameter_REG<<8)|electric[3];
    		  Voltage_parameter_REG = (Voltage_parameter_REG<<8)|electric[4];

    		  Voltage_REG =  electric[5];
    		  Voltage_REG = (Voltage_REG<<8)|electric[6];
    		  Voltage_REG = (Voltage_REG<<8)|electric[7];

    		  Current_PParameter_REG =  electric[8];
    		  Current_PParameter_REG = (Current_PParameter_REG<<8)|electric[9];
    		  Current_PParameter_REG = (Current_PParameter_REG<<8)|electric[10];

    		  Current_REG =  electric[11];
    		  Current_REG = (Current_REG<<8)|electric[12];
    		  Current_REG = (Current_REG<<8)|electric[13];

    		  Power_parameter_REG =  electric[14];
    		  Power_parameter_REG = (Power_parameter_REG<<8)|electric[15];
    		  Power_parameter_REG = (Power_parameter_REG<<8)|electric[16];

    		  Power_REG =  electric[17];
    		  Power_REG = (Power_REG<<8)|electric[18];
    		  Power_REG = (Power_REG<<8)|electric[19];

    		  Data_Updata_REG = electric[20];

    		  PF_REG = electric[21];
    		  PF_REG = (PF_REG<<8)|electric[22];

    		  CheckSum_REG = electric[23];
    	  }
    	  float _VOLTAGE = 3.48;
    	  float _CURRENT = 1.35;

    	  //有效电压 = 电压参数寄存器/电压寄存器*电压系数
    	  all_data.voltage = (Voltage_parameter_REG/Voltage_REG)*_VOLTAGE;
    	  //有效电流 = 电流参数寄存器/电流寄存器 *电流系数
    	  all_data.current = (Current_PParameter_REG/Current_REG)*_CURRENT;
    	  //有功功率 = 功率参数寄存器/功率寄存器 × 电压系数 × 电流系数
    	  all_data.active_power = (Power_parameter_REG/Power_REG)*_VOLTAGE*_CURRENT;
    	  //视在功率 = 有效电压 × 有效电流
    	  all_data.apparent_power =  all_data.voltage*all_data.current;
    	  //功率因素 = 有功功率/视在功率
    	  all_data.power_factor = all_data.active_power/ all_data.apparent_power;

    	  //记录取反次数
    	  static uint8_t _PF;
    	  static uint32_t pf_k = 0;

    	  if(_PF != electric[21]>>7)
    	  {
    		  pf_k++;
    		  _PF = electric[21]>>7;
    	  }
    	  //PFcnt = k ∗ 65536 + n     1 度电的脉冲数量 = 1/功率参数寄器*1/电压系数 × 电流系数 × 10*9次方 × 3600    当PFcnt 的个数等于 1 度电的脉冲数量时，表示己累计统计 1 度电量
    	  all_data.total_kwh +=(pf_k*65536+PF_REG)/((1/Power_parameter_REG)*1000000000*3600);
    //  }
//      else
//	   {
//		   all_data.voltage = 0;
//		   all_data.current = 0;
//		   all_data.active_power = 0;
//		   all_data.apparent_power = 0;
//		   all_data.power_factor = 0;
//	   }

	  electric_cnt = 0;
  }

}

